package com.wonyesoft.wuliu.acticity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionResult.SuggestionInfo;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.CarType;
import com.wonyesoft.wuliu.model.RealTimePosition;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class LbsDriversActivity extends BaseActivity implements
		OnClickListener {

	private TextView title, head_right, cargoinfo_cargotypetxt,
			cargoinfo_cargovolumetxt, cargoinfo_cargoweighttxt,
			cargoinfo_cargocartypetxt;
	private ImageView addcargo_cancle;
	private EditText driver_carnumber;
	private ListView addcargo_listmap;
	private SimpleAdapter adapter;
	private ImageView head_left;
	// private SuggestionSearch mSuggestionSearch;
	private String seachWords;
	private MapView mMapView = null;
	private BaiduMap mBaiduMap = null;
	private BitmapDescriptor bitmap;
	private RequestParams params;
	private AppContent app;
	private SuggestionSearch mSuggestionSearch;
	private List<Map<String, Object>> list;
	private HashMap<String, String> mapm = null;
	private int resultCode;
	private String driverid = null;
	private RealTimePosition realTimePosition;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(AppContent.getContext());
		app = (AppContent) getApplication();
		setContentView(R.layout.aj_driversmap);
		title = (TextView) findViewById(R.id.head_title);
		title.setText("定位追踪");
		head_left = (ImageView) findViewById(R.id.head_left);
		head_left.setOnClickListener(this);
		head_left.setVisibility(0);
		driverid = getIntent().getStringExtra("driverId");
		//getDriverLbs();
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		getDriverLbs();
	}

	private void getDriverLbs() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userType", "司机");
		dataMap.put("userId",driverid);
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
//		http.configCookieStore(preferencesCookieStore);
		http.send(HttpRequest.HttpMethod.POST, GagApi.GETPOSITON, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						System.out.println("进入getcartype");
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();
						// System.out.println(preferencesCookieStore);
						if (state.getCode().equals(State.SUCEECCODE)) {

							if (gson2 != null) {
								Type type = new TypeToken<RealTimePosition>() {
								}.getType();
								try {
									realTimePosition = gson2.fromJson(
											new JSONObject(responseInfo.result)
													.getJSONObject("data")
													.getJSONObject("rows")
													+ "", type);
								} catch (JsonSyntaxException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (null != realTimePosition)
									addBaiduMapPoint();

							} else
								System.out.println(state.getMsg());

						} else
							// System.out.println(state.getMsg());
							showLongToast(state.getMsg());
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// testTextView.setText(error.getExceptionCode() + ":" +
						// msg);
						System.out.println(msg);
					}
				});
	}

	private List<Map<String, Object>> getData(
			List<SuggestionInfo> allSuggestions) {
		System.out.println("-----START-----");
		for (SuggestionInfo c : allSuggestions) {
			HashMap<String, Object> map = new HashMap<String, Object>();;
			System.out.println("ket:" + c.key);
			map.put("a0_common", c.key);
			map.put("a1_common", c.city + c.district + c.key);

			list.add(map);
		}
		System.out.println("-----END-----");
		
		return list;
	}

	Intent tochangeIntent;
	Bundle bundle;


	private void addBaiduMapPoint() {
		
		// mBaiduMap.setTrafficEnabled(true);
		// mBaiduMap.setBaiduHeatMapEnabled(true);
		System.out.println(app.adrr);
		System.out.println(app.latitude);
		System.out.println(app.longitude);
		// 定义Maker坐标点
		LatLng point = new LatLng(Double.valueOf(realTimePosition.getLatitude()), Double.valueOf(realTimePosition.getLongitude()));
		LatLng point1 = new LatLng(app.latitude + 1, app.longitude);
		LatLng point2 = new LatLng(app.latitude - 1, app.longitude);
		// 构建Marker图标
		bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
		MyLocationConfiguration config = new MyLocationConfiguration(null,
				true, BitmapDescriptorFactory.fromResource(R.drawable.icon_geo));
		mBaiduMap.setMyLocationConfigeration(config);
		// 构建MarkerOption，用于在地图上添加Marker
		OverlayOptions option = new MarkerOptions().position(point)
				.icon(bitmap);
		// 在地图上添加Marker，并显示
		mBaiduMap.addOverlay(option);
		// = MapStatusUpdateFactory.newLatLng(point);
		 MapStatusUpdate u =MapStatusUpdateFactory.newLatLngZoom(point, 17);
		mBaiduMap.animateMapStatus(u);
		mBaiduMap.setMyLocationEnabled(false);

	}

	protected void onDestroy() {
		super.onDestroy();
		// 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
		mMapView.onDestroy();
	}

	protected void onResume() {
		super.onResume();
		// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
		mMapView.onResume();
	}

	protected void onPause() {
		super.onPause();
		// 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
		mMapView.onPause();
	}

	

	protected void getmap(String seachWords2) {

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.addcargo_cancle:
		case R.id.head_left:
			finish();
			break;
		case R.id.driver_baochun:
			break;
		default:
			break;
		}
	}
}
